<!DOCTYPE html>
<html lang="ch-cn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>
    <link rel="shortcut icon" href="./images/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="./css/base-ui.css">
    <link rel="stylesheet" href="./css/message.css">
    <script src="./js/jquery-3.4.1.min.js"></script>
    <script src="./bootstrap-3.4.1/js/bootstrap.min.js"></script>
    <script src="./js/cookie.js"></script>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        body {
            width: 100%;
            height: 100vh;
        }

        main {
            height: 100%;
            width: 100%;
            display: flex;
            font-family: MiSans,sans-serif;
        }

        .image-background {
            width: 375px;
            background-image: url("./images/login/bg.jpg");
            background-size: cover;
            background-position: center;
            background-repeat: no-repeat;
        }

        .account-body {
            width: calc(-375px + 100%);
            background-color: var(--color-background-soft);
            position: relative;
        }

        .account-card {
            width: 400px;
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            background-color: var(--color-background);
            border-radius: 5px;
            box-shadow: var(--shadow2);
            overflow: hidden;
            padding: 20px;
        }

        .account-card .title nav {
            display: flex;
            column-gap: 20px;
        }

        .account-card .title nav a {
            color: var(--ui-colors-neutral9);
        }

        .account-card .title nav a.active {
            color: black;
        }

        .account-card .account-content {
            width: 770px;
            display: flex;
            column-gap: 25px;
            overflow: hidden;
            transition: all 0.35s ease-in-out;
        }

        .link-decorate {
            width: 45px;
            height: 4px;
            border-radius: 2px;
            background-color: var(--ui-colors-orange);
            transform: translateX(1px);
            transition: all 0.35s ease-in-out;
        }

        #type:checked+.link-decorate {
            transform: translateX(69px);
        }

        .account-card .account-content {
            transform: translateX(0px);
            margin: 15px 0;
        }

        #type:checked~.account-content {
            transform: translateX(-385px);
        }

        .account-content .login,
        .account-content .register {
            width: 360px;
        }

        .account-input {
            width: 100%;
            font-family: 'Segoe UI', sans-serif;
            margin: 1em 0 1em 0;
            position: relative;
            background-color: var(--ui-colors-neutral2);
        }

        .account-input input {
            font-size: 100%;
            padding: 0.8em;
            outline: none;
            border: 2px solid var(--color-none);
            background-color: transparent;
            border-radius: 5px;
            width: 100%;
        }

        .account-input label {
            font-size: 100%;
            position: absolute;
            left: 0;
            padding: 0.8em;
            margin-left: 0.5em;
            pointer-events: none;
            transition: all 0.3s ease;
            color: var(--ui-colors-neutral10);
            font-weight: 500;
        }

        .account-input :is(input:focus, input:valid)~label {
            transform: translateY(-50%) scale(.9);
            margin: 0em;
            margin-left: 1.3em;
            padding: 0.4em;
        }

        .account-input :is(input:focus, input:valid) {
            border-color: var(--ui-colors-orange);
        }

        .block {
            display: none;
            height: 4px;
            width: 60px;
            background-color: var(--color-background);
            position: absolute;
            top: -1px;
            left: 20px;
            transition: all 0.35s ease-in-out;
        }
        .account-input :is(input:focus, input:valid) ~ .block {
            display: inline;
        }

        .account-content .bottom {
            width: 100%;
        }

        .account-content .bottom button {
            width: 100%;
            height: 60px;
            background-color: var(--ui-colors-orange);
            border-radius: 3px;
            font-size: 20px;
            font-weight: 600;
            color: var(--color-background);
        }

        .forget {
            margin: 20px 0 60px 5px;
        }

        .forget a {
            color: var(--ui-colors-neutral10);
        }

        .forget a:hover {
            color: var(--ui-colors-orange);
        }

        @media screen and (max-width: 1100px) {
            .image-background {
                width: 200px;
            }

            .account-body {
                width: calc(-200px + 100%);
            }
        }

        @media screen and (max-width: 1000px) {
            .image-background {
                width: 0px;
            }

            .account-body {
                width: 100%;
            }
        }
    </style>
</head>
<body>
    <main>
        <div class="image-background"></div>
        <div class="account-body">
            <div class="account-card">
                <div class="title">
                    <nav>
                        <a href="#login" id="link-login">
                            <h2>登录</h2>
                        </a>
                        <a href="#register" id="link-register">
                            <h2>注册</h2>
                        </a>
                    </nav>
                </div>
                <input type="checkbox" id="type" style="display: none;">
                <div class="link-decorate"></div>
                <div class="account-content" style="background: var(--color-none);">
                    <div class="login">
                        <form class="" role="form">
                            <div>
                                <div class="account-input">
                                    <input type="text" name="login-username" id="login-username" 
                                    class="form-control" required="required" title="">
                                    <div class="block"></div>
                                    <label for="login-username">
                                        用户名
                                    </label>
                                </div>
                            </div>
                            <div>
                                <div class="account-input">
                                    <input type="password" name="login-password" id="login-password" 
                                    class="form-control" required="required" title="">
                                    <div class="block" style="width: 46px;"></div>
                                    <label for="login-password">密码</label>
                                </div>
                            </div>
                            <div class="forget">
                                <a href="/forget">
                                    忘记密码？
                                </a>
                            </div>
                        </form>
                        <div class="bottom">
                            <button onclick="TOLogin()">登录</button>
                        </div>
                    </div>
                    <div class="register">
                        <form class="" role="form">
                            <div>
                                <div class="account-input">
                                    <input type="text" name="register-username" id="register-username" 
                                    class="form-control" required="required" title="">
                                    <div class="block"></div>
                                    <label for="register-username">
                                        用户名
                                    </label>
                                </div>
                            </div>
                            <div>
                                <div class="account-input">
                                    <input type="text" name="telephone" id="telephone" 
                                    class="form-control" required="required" title="">
                                    <div class="block" style="width: 77px;"></div>
                                    <label for="telephone">
                                        电话号码
                                    </label>
                                </div>
                            </div>
                            <div>
                                <div class="account-input">
                                    <input type="password" name="register-password" id="register-password" 
                                    class="form-control" required="required" title="">
                                    <div class="block" style="width: 46px;"></div>
                                    <label for="register-password">密码</label>
                                </div>
                            </div>
                            <div>
                                <div class="account-input">
                                    <input type="password" name="confirm-password" id="confirm-password" 
                                    class="form-control" required="required" title="">
                                    <div class="block" style="width: 77px;"></div>
                                    <label for="confirm-password">确认密码</label>
                                </div>
                            </div>
                        </form>
                        <div class="bottom">
                            <button onclick="reg()">注册</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </main>
</body>
<script src="./js/message.js"></script>
<script>
    document.getElementById("login-username").value = GetCookie("user") 

    const type = document.getElementById("type")
    const login = document.getElementById("link-login")
    const register = document.getElementById("link-register")
    function changeCon() {
        var hash = window.location.hash
        register.classList.remove("active")
        login.classList.remove("active")
        if (hash === '#register') {
            type.checked=true
            register.classList.add("active")
        }
        else {
            type.checked=false
            login.classList.add("active")
        }
    }
    window.addEventListener('load', function() {
        changeCon()
    });
    window.addEventListener('hashchange', () => {
        changeCon()
    });
    // 登录
    function TOLogin(){
        let username = $("#login-username").val();//获取用户名
        let password = $("#login-password").val();//获取密码
        if(username.length>0 &&   password.length>0){//判断用户名密码是否填写
            $.ajax({//使用ajax发送用户数据，进行登陆验证
                url:"/api/login",
                method:"post",
                data:{
                    username: username,
                    password: password
                },
                success(res){
                    if(res.code===1001) {
                        AddMessage("success", "登录成功")
                        SetCookie("user", username)
                        SetCookie("id", res.id)
                        SetCookie("token", res.token)
                        setTimeout(()=>{
                            location.href = "/"
                        }, 500)
                    } else {
                        AddMessage("warning", "用户名或密码错误")
                    }
                }
            })
        }else{
            AddMessage("warning", "用户名和密码必填")
        }
    }
    // 注册
    function reg(){
        //获取用户填写的个人信息
        let username=$("#register-username").val()
        let tel=$("#telephone").val()
        let password_one=$("#register-password").val();
        let password_two=$("#confirm-password").val();
        let namereg=/^[\u4e00-\u9fa5]{0,}$/;
        let telreg=/(13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/;
        // 此处验证可通过正则匹配
        if(password_one!==password_two) {
            AddMessage("warning", "2次密码输入不一致")
        } else {//验证数据是否符合要求
            if (namereg.test(username)) {
                if (telreg.test(tel)) {
                    if (password_one.length >= 6 && password_one.length <= 10) {
                        $.ajax({//使用ajax发送用户数据，进行注册验证
                            url:"/api/register",
                            method:"post",
                            data:{
                                username:username,
                                password:password_one,
                                tel:tel
                            },
                            success(res){
                                if(res.code===1001){
                                    AddMessage("success", res.msg)
                                    window.location.hash="#login"
                                } else {
                                    AddMessage("warning", res.msg)
                                }
                            }
                        })
                    } else {
                        AddMessage("warning", "密码长度应在6-20个字符之间")
                    }
                } else {
                    AddMessage("warning", "手机号码格式不合法")
                }
            } else {
                AddMessage("warning", "用户名只可是中文")
            }
        }
    }
</script>
</html>